<?php
/**
 * Created by PhpStorm.
 * User: JunHui.Li
 * Date: 15-2-5
 * Time: 下午13:52
 */
class printCode{


    public function getCodeList(&$arg_array){


        global $G_PGLOB;
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //订单编号
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['bill_code'];
        }

        //配送日期
        if (isset($arg_array['send_date']) && !empty($arg_array['send_date'])) {
            $sql .= ' AND send_date = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_date'];
        }


        //配送站
        if (isset($arg_array['distribution_id']) && !empty($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }

        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_master JOIN activities_extend ON bill_master.bill_code= activities_extend.extend_1' . $sql;
                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            $sql=str_replace("WHERE","HAVING",$sql);

            //查询分类信息
            //$search_sql = "SELECT a.*,GROUP_CONCAT((CONCAT(b.`goods_name`,'：', b.`goods_amount`)) SEPARATOR '，')AS product_detail  FROM  bill_master a LEFT JOIN `bill_goods` b ON a.bill_no=b.bill_no GROUP BY a.bill_no " . $sql;

            $search_sql = "SELECT * FROM bill_master JOIN activities_extend ON bill_master.bill_code= activities_extend.extend_1"  . $sql;
            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'bill_code' => $item->bill_code,
                    'send_date' => $item->send_date,

                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }



    }



}










